import requests

import json

# 请求配置（修正参数异常分隔）
url = "https://applog.longhuvip.com/w1/api/index.php"
query_params = {
    "apiv": "w398",  # 分离异常参数
    "PhoneOSNew": "18",  # 操作系统版本参数
    "VerSion": "5.18.0.2"  # 修正/erSion为VerSion（保持原始大小写）
}

headers = {
    "Host": "applog.longhuvip.com",
    "Connection": "keep-alive",
    "sec-ch-ua-platform": "\"Android\"",
    "User-Agent": "Mozilla/5.0 (Linux; Android 15; 2410DPN6CC Build/AQ3A.240812.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/134.0.6998.108 Mobile Safari/537.36;kaipanla 5.18.0.2",
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "sec-ch-ua": "\"Chromium\",v=\"134\", \"Not:A-Brand\",v=\"24\", \"Android WebView\",v=\"134\"",  # 保持原始逗号格式
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "sec-ch-ua-mobile": "?1",
    "Origin": "https://apppage.longhuvip.com",
    "X-Requested-With": "com.aiyu.kaipanla",
    "Sec-Fetch-Site": "same-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://apppage.longhuvip.com/",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9;en-US;q=0.8,en;q=0.7"
}

# 修正请求体参数（分离Param0和Param1）
payload = {
    "c": "DataStatistics",
    "a": "CallSerClick",
    "DeviceID": "04fedbfcb71c68a1",  # 设备指纹
    "Param0": "98",  # 独立参数
    "Param1": "1"  # 修正参数分隔问题
}


def send_analytics_request():
    try:
        with requests.Session() as s:
            # 禁用SSL验证（仅测试环境使用）
            s.verify = False

            # 发送请求
            response = s.post(
                url=url,
                params=query_params,
                headers=headers,
                data=payload,
                timeout=10
            )

            # 处理特殊压缩
            if 'zstd' in response.headers.get('Content-Encoding', ''):
                try:
                    import zstandard as zstd
                    dctx = zstd.ZstdDecompressor()
                    decompressed = dctx.decompress(response.content)
                    result = json.loads(decompressed.decode('utf-8'))
                except ImportError:
                    print("检测到Zstandard压缩，请先安装zstandard库：pip install zstandard")
                    return None
            else:
                result = response.json()

            # 验证业务状态码
            if result.get("code") == 0:  # 需确认实际成功状态码
                print("数据上报成功！")
                return result.get("data", {})
            else:
                print(f"业务异常：{result.get('msg', '未知错误')}")
                return None

    except Exception as e:
        print(f"请求失败：{type(e).__name__} - {str(e)}")
        return None


# 调试执行
if __name__ == "__main__":
    print("正在发送数据统计请求...")
    response_data = send_analytics_request()

    if response_data:
        print("\n响应数据结构：")
        print(json.dumps(response_data, indent=2, ensure_ascii=False))
    else:
        print("请求未返回有效数据")